﻿#define _CRT_SECURE_NO_WARNINGS  1
//一定要动手，例子不管多么简单，建议至少自己手敲一遍看看是否理解了里头的细枝末节。
//一定要学会思考，思考为什么要这样，而不是那样。还要举一反三地思考
#include"work.h"
//打印
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	/*int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		return 29;
	}
	else {
		return day[month];*/
	assert(year >= 1 && month >= 1 && month <= 12);

	int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };

	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		return 29;

	return monthArray[month];
}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
	return *this;
}
// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		this->_day -= (-day);
	}
	this->_day += day;
	while (this->_day > GetMonthDay(this->_year, this->_month))
	{
		this->_day -= GetMonthDay(this->_year, this->_month);
		this->_month++;
		if (this->_month > 12)
		{
			this->_year++;
			this->_month = 1;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	this->_day -= day;
	while (this->_day <1)
	{
		this->_month--;
		if (this->_month <1)
		{
			this->_year--;
			this->_month = 12;
		}
		this->_day += GetMonthDay(this->_year, this->_month);
		
	}
	return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}


// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;

}
// 后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	tmp += 1;
	return tmp;
}
// 前置--
Date& Date::operator--()
{
	*this-= 1;
	return *this;
}
// 后置--
Date Date::operator--(int)
{
	Date tmp(*this);
	tmp -= 1;
	return tmp;
}

// >运算符重载
bool Date::operator>(const Date& d)
{
	if (this->_year > d._year)
	{
		return true;
	}
	if (this->_year == d._year && this->_month > d._month)
	{
		return true;
	}
	if (this->_year == d._year && this->_month == d._month && this->_day > d._day)
	{
		return true;
	}
	return false;
}
// ==运算符重载
bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
// >=运算符重载
bool Date::operator >= (const Date& d)
{
	return *this > d || *this == d;
}
// <运算符重载
bool Date::operator < (const Date& d)
{
	return !(*this >= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
	return !(*this > d);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	//假设左边大
	int flage = 1;
	Date max = *this;
	Date min = d;
	int n = 0;
	//假设错误，
	if (*this < d)
	{
		flage = -1;
		max = d;
		min = *this;
	}
	
	while (max != min)
	{
		cout << min._year << "年" << min._month << "月" << min._day << "日" << endl;
		++min;
		++n;
	}
	return n * flage;
}
